package com.example.answer_staff;

import java.util.ArrayList;

import org.apache.http.Header;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

import com.example.answer_staff.api.ApiManager;
import com.gaosai.staff.commlistadapter.ShowActivityAdapter;
import com.gaosai.staff.domain.MessageResult;
import com.gaosai.staff.utils.JsonUtils;
import com.gaosai.staff.utils.SPUtil;
import com.gaosai.staff.utils.UseCommInterface;
import com.gaosai.staff.xlistview.XListView;
import com.gaosai.staff.xlistview.XListView.IXListViewListener;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class CampaignActivity  extends BaseActivity implements IXListViewListener{


	public static final int CAMP_REQUEST = 2;
	
	private int page = 1 ;
	private int pagesize =10 ;
	private String keyWord="";
	private String tagNames="";

	private XListView listview;
	private ShowActivityAdapter showactivityAdapter;
	private ArrayList<String> showActivity_list;
	//	private String barCode_share;
	//	private String[] barcode_string;



	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.campain_listview);
		setContentViewItem(R.layout.campain_listview) ;
		setupView();
		setListeneers();
		getActivityData();

	}
	private void setListeneers() {

		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {


				Intent intent=new Intent(CampaignActivity.this,ListCampDetailsActivity.class);

				intent.putExtra("new", showActivity_list.get(position-1));
				//				startActivity(intent);	
				startActivity(intent) ;
			}
		});
	}
	private void setupView() {
		listview=(XListView)findViewById(R.id.listView_activities);
		listview.setPullRefreshEnable(true);
		listview.setPullLoadEnable(true);
		listview.setXListViewListener(this) ;
		showActivity_list=new ArrayList<String>();
		showactivityAdapter=new ShowActivityAdapter(CampaignActivity.this,showActivity_list);
		listview.setAdapter(showactivityAdapter);
		setTitleName("Activities") ;
		showAllImage(R.drawable.menu, R.drawable.filter, R.drawable.me, alignRightOnClickListener, rightOnClickListener) ;
	
	}
	private View.OnClickListener rightOnClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			
		}
	};
	
	private View.OnClickListener alignRightOnClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			UseCommInterface.getInstance().startActivityForResult(CampaignActivity.this, CAMP_REQUEST, FliterActivity.class, null) ;
		}
	};
	@Override
	public void onRefresh() {
		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				page = 1 ;
				showActivity_list.clear() ;
				getActivityData() ;
				listview.stopLoadMore() ;
				listview.stopRefresh() ;

			}
		}, 2000) ;
	}
	protected void getActivityData() {
		Log.e("camp的tagnames", "tagNames是"+tagNames);
		ApiManager.getInstance().adminshow(CampaignActivity.this,String.valueOf(page),String.valueOf(pagesize),keyWord,tagNames,new AsyncHttpResponseHandler() {
			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				Log.e("eee", "CampActivity开始向服务器请求数据");
				String result = new String(arg2) ;
				Log.e("huo的的的 dong sssssssss", result) ;
				MessageResult messageResult = MessageResult.prase(result) ;
				if(0==messageResult.getCode()){
					showActivity_list.addAll(JsonUtils.JsonShowActivityResult(messageResult.getData())) ;
					showactivityAdapter.notifyDataSetChanged() ;
					System.out.println("news_list是"+showActivity_list);
				}else{
					Toast.makeText(CampaignActivity.this, messageResult.getMsg(), 0).show() ;
				}
			}
			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
				Log.e("fffffffffffffffffffffff", "666666") ;
			}
		}) ;
	}
	@Override
	public void onLoadMore() {

		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				page++ ;
				getActivityData() ;
				listview.stopRefresh() ;
				listview.stopLoadMore() ;
			}
		}, 2000) ;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Log.e("eee", "CampActivityForResult"+requestCode);
		if(resultCode==RESULT_OK){
			//再判断返回来的情况，是成功还是失败等等	 resultCode
			Log.e("eee", "newsroom中的resultCode"+requestCode);
			switch(requestCode){
			case CAMP_REQUEST:
				String tagNames_list = SPUtil.getValue(getApplicationContext(), SPUtil.TAGNAMES);
				String tagNames_list_substring = tagNames_list.substring(tagNames_list.indexOf("[")+1,tagNames_list.lastIndexOf("]"));
				Spilt(tagNames_list_substring);
				Log.e("eee", "Spilt的tagename是"+tagNames);	
				page =1 ;
				showActivity_list.clear() ;
				getActivityData();	
				break;
			}
		}
	}
	private void Spilt(String tagNames_list_substring) {

		StringBuilder sb=new StringBuilder();
		String[] tagNames_list_spilt = tagNames_list_substring.split(",");
		for (String string : tagNames_list_spilt) {
			String string_spilt = string;
			sb.append(string_spilt.trim()).append(",");
		}
		tagNames=sb.deleteCharAt(sb.length()-1).toString();
	}

}
